<?php

class Service_CustomerController extends Custom_Controller_Action
{
    public function indexAction()
    {
        $objCustomer = new Service_Model_DbTable_Customer();
        $page = $this->getRequest()->getParam('page');
		$objCustomer->setPaginator($this->Config->params->recordsPerPage,$page);
        $this->setSort($objCustomer);
        $customers = $objCustomer->fetchAllWithBank($this->auth->getIdentity()->bank_id, $this->searchKey);
        $this->view->customers = $customers;
		$this->view->paginator = $objCustomer->getPaginator();
		$this->view->page = $page;
        $this->_setTab(1);
    }

    public function listsAction()
    {
        $objCustomer = new Service_Model_DbTable_Customer();
        $page = $this->getRequest()->getParam('page');
		$objCustomer->setPaginator($this->Config->params->recordsPerPage,$page);
        $this->setSort($objCustomer);
        $customers = $objCustomer->fetchAllWithBank(null, $this->searchKey);
        $this->view->customers = $customers;
		$this->view->paginator = $objCustomer->getPaginator();
		$this->view->page = $page;
        $this->_setTab(0);
    }

    public function addAction()
    {
        $form = new Service_Form_Customer();
        $this->view->form = $form;

		if($this->getRequest()->isPost())
		{
			$objService	= new Service_Model_DbTable_Customer();
			$formData = $this->getRequest()->getPost();

			if($form->isValid($formData))
			{
                $values = $form->getValues();
                $values['bank_id'] = $this->auth->getIdentity()->bank_id;
                $newId = $objService->insert($values);

                if($newId)
                {
                    $this->addMessage("Customer added successfully");
                    $this->RedirectUrl("customer/add");
                }
                else
                {
                    $this->addMessage("Customer not added successfully", 'error');
                }
            }
        }
        
        $this->_setTab(2);
    }

    public function editAction()
    {
        $cid = $this->_getParam('cid');

        if($cid)
        {
            $form = new Service_Form_Customer();
            $id = Custom_General::decode($cid);
            $objCustomer = new Service_Model_DbTable_Customer();
            
            if($this->getRequest()->isPost())
            {
                $formData = $this->getRequest()->getPost();

                if($form->isValid($formData))
                {
                    $values = $form->getValues();
                    if(empty($values['image'])) unset ($values['image']);
                    $res = $objCustomer->updateById($values, $id);

                    if($res)
                    {
                        $this->addMessage("Customer information updated successfully");
                        $this->RedirectUrl($this->getRequest()->getServer('HTTP_REFERER'));
                    }
                    else
                    {
                        $this->addMessage("Customer information not updated successfully", 'error');
                    }
                }
            }

            $customer = $objCustomer->fetchOneWithBank($id);
           
            if($customer)
            {
                $form->populate($customer);
                $this->view->form = $form;
            }
        }

        $this->_setTab(3);
    }

    public function deleteAction()
    {
        $cid = $this->_getParam('cid');

		if($cid)
		{
			$objCustomer = new Service_Model_DbTable_Customer();
            $id = Custom_General::decode($cid);
			$res = $objCustomer->deleteById($id);

			if($res)
				$this->addMessage('Customer delete successfully');
			else
				$this->addMessage('Customer not delete successfully','error');

			if($this->_getParam('page'))
				$params = array('page'=>$this->_getParam('page'));
			else
				$params = array();

			$this->RedirectUrl('customers',$params);
		}
    }

    public function showAction()
	{
		$cid = $this->_getParam('cid');

		if($cid)
		{
			$id = Custom_General::decode($cid);

			$objCustomer= new Service_Model_DbTable_Customer();
            $objBank = new Service_Model_DbTable_Bank();
            $objDistrict = new Service_Model_DbTable_District();
			$data = $objCustomer->getOne($id);
            $bank = $objBank->getOne($data['bank_id']);
            $district = $objDistrict->getOne($data['citizenship_district_id']);
            $data['Bank'] = $bank;
            $data['District'] = $district;
            $this->view->customer = $data;
			$this->view->customerImagePath = $this->Config->params->bank->imagePath;
		}
		else
		{
			$this->view->customer = false;
		}

        $this->_setTab(4);
	}

    private function _setTab($activeIndx)
	{
		$subItems = array(__('All customers'), __('My customers'),__('Add Customer'),__('Edit Customer'),__('Customer Details'));
		$subItemLinks = array('all/customers','my/customers','customer/add','customer/edit', '#');
		$linkRoutes = array('cIndex','cLists','cAdd','cEdit', 'cShow');

		$menues = array('0','1','2');

		if(!in_array($activeIndx, $menues)) $menues[] = $activeIndx;

		$subMenues = array('items' => $subItems, 'links'=> $subItemLinks,
						   'enableMenues' => $menues, 'activeIndx' => $activeIndx);

		$this->view->subMenues = $subMenues;
	}
}